// Object to simplify storing and accessing dimensions of a 3D object (such as parcel)
public class Dimensions {
	/**
	 * @uml.property  name="length"
	 */
	private double length;
	/**
	 * @uml.property  name="width"
	 */
	private double width;
	/**
	 * @uml.property  name="height"
	 */
	private double height;

	// Create Dimensions object from three double inputs
	public Dimensions(double length, double width, double height) {
		this.length = length;
		this.width = width;
		this.height = height;
	}

	// Create Dimensions object from String in format "L x W x H"
	public Dimensions (String line) {
		try {
			// Tokenise input string based on entities being separated by spaces
			String[] tokens = line.split(" ");
			// Create array to store three Double dimensions
			Double[] dimensions = new Double[3];
			// Loop through tokens
			int count = 0;
			for(String token : tokens){
				// Ignore 'x', as that is the desired separator
				if(!token.equalsIgnoreCase("x")) {
					dimensions[count] = Double.parseDouble(token);
					count++;
				}
			}

			// First part before first space is assumed to be numerical length
			this.length = dimensions[0];
			// Third part is assumed to be numerical width
			this.width = dimensions[1];
			// Fifth part is assumed to be numerical height
			this.height = dimensions[2];
		}

		//this catches trying to convert a String to an integer
		catch (NumberFormatException nfe) {
			String error = "Number conversion error in '" + line + "'  - " + nfe.getMessage();
			error += "\n\nPlease check the dimensions in the Parcel input file; dimensions should be in the following format:\n";
			error += "L x W x H\n";
			error += "Where L,W,H are positive real numbers.\n";
			System.out.println(error);
			System.exit(1);
		}
		//this catches missing items if only one or two items
		//other omissions will result in other errors
		catch (ArrayIndexOutOfBoundsException air) {
			String error = "Not enough items in  : '" + line + "' index position : " + air.getMessage();
			error += "\n\nPlease check the dimensions in the Parcel input file; dimensions should be in the following format:\n";
			error += "L x W x H\n";
			error += "Where L,W,H are positive real numbers.\n";
			System.out.println(error);
			System.exit(1);
		}
	}

	/**
	 * @return
	 * @uml.property  name="length"
	 */
	public double getLength() {
		return length;
	}
	/**
	 * @return
	 * @uml.property  name="width"
	 */
	public double getWidth() {
		return width;
	}
	/**
	 * @return
	 * @uml.property  name="height"
	 */
	public double getHeight() {
		return height;
	}

	/**
	 * @param  length
	 * @uml.property  name="length"
	 */
	public void setLength(double length) {
		this.length = length;
	}
	/**
	 * @param  width
	 * @uml.property  name="width"
	 */
	public void setWidth(double width) {
		this.width = width;
	}
	/**
	 * @param  height
	 * @uml.property  name="height"
	 */
	public void setHeight(double height) {
		this.height = height;
	}

	public double getVolume() {
		return length * width * height;
	}

	/**
	 * @uml.property  name="parcel"
	 * @uml.associationEnd  inverse="dimensions1:Parcel"
	 */
	private Parcel parcel;

	/**
	 * Getter of the property <tt>parcel</tt>
	 * @return  Returns the parcel.
	 * @uml.property  name="parcel"
	 */
	public Parcel getParcel() {
		return parcel;
	}

	/**
	 * Setter of the property <tt>parcel</tt>
	 * @param parcel  The parcel to set.
	 * @uml.property  name="parcel"
	 */
	public void setParcel(Parcel parcel) {
		this.parcel = parcel;
	}

}

